﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using eShop.DataAccess.Entity;

namespace eShop.DataAccess
{
    public partial class OrderItemDAL : BaseDAL
    {
        public static bool Insert(OrderItem orderItem)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("OrderItem_InsertOrUpdate");
            db.AddOutParameter(dbCommand, "Id", DbType.Int32, 4);
            db.AddInParameter(dbCommand, "OrderId", DbType.Int32, orderItem.OrderId);
            db.AddInParameter(dbCommand, "ProductId", DbType.Int32, orderItem.ProductId);
            db.AddInParameter(dbCommand, "ProductName", DbType.String, orderItem.ProductName);
            db.AddInParameter(dbCommand, "ProductCode", DbType.String, orderItem.ProductCode);
            db.AddInParameter(dbCommand, "ProductDescription", DbType.String, orderItem.ProductDescription);
            db.AddInParameter(dbCommand, "ProductDefaultImage", DbType.String, orderItem.ProductDefaultImage);
            db.AddInParameter(dbCommand, "StockId", DbType.Int32, orderItem.StockId);
            db.AddInParameter(dbCommand, "StockDescription", DbType.String, orderItem.StockDescription);
            db.AddInParameter(dbCommand, "UnitPrice", DbType.Double, orderItem.UnitPrice);
            db.AddInParameter(dbCommand, "UnitWeight", DbType.Double, orderItem.UnitWeight);
            db.AddInParameter(dbCommand, "Quantity", DbType.Int32, orderItem.Quantity);

            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                orderItem.Id = Convert.ToInt32(db.GetParameterValue(dbCommand, "@Id"));
                return true;
            }
            return false;
        }

        public static bool Update(OrderItem orderItem)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("OrderItem_InsertOrUpdate");
            db.AddInParameter(dbCommand, "Id", DbType.Int32, orderItem.Id);
            db.AddInParameter(dbCommand, "OrderId", DbType.Int32, orderItem.OrderId);
            db.AddInParameter(dbCommand, "ProductId", DbType.Int32, orderItem.ProductId);
            db.AddInParameter(dbCommand, "ProductName", DbType.String, orderItem.ProductName);
            db.AddInParameter(dbCommand, "ProductCode", DbType.String, orderItem.ProductCode);
            db.AddInParameter(dbCommand, "ProductDescription", DbType.String, orderItem.ProductDescription);
            db.AddInParameter(dbCommand, "ProductDefaultImage", DbType.String, orderItem.ProductDefaultImage);
            db.AddInParameter(dbCommand, "StockId", DbType.Int32, orderItem.StockId);
            db.AddInParameter(dbCommand, "StockDescription", DbType.String, orderItem.StockDescription);
            db.AddInParameter(dbCommand, "UnitPrice", DbType.Double, orderItem.UnitPrice);
            db.AddInParameter(dbCommand, "UnitWeight", DbType.Double, orderItem.UnitWeight);
            db.AddInParameter(dbCommand, "Quantity", DbType.Int32, orderItem.Quantity);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        public static List<OrderItem> GetAll(Boolean allowPaging = false, Int32 pageIndex = 0, Int32 pageSize = 0, String orderColumn = "Id", Boolean isAscending = false)
        {
            List<OrderItem> orderItemList = new List<OrderItem>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("OrderItem_GetAll");
            db.AddInParameter(dbCommand, "AllowPaging", DbType.Boolean, allowPaging);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbCommand, "OrderColumn", DbType.String, orderColumn);
            db.AddInParameter(dbCommand, "IsAscending", DbType.Boolean, isAscending);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                orderItemList = GetEntitiesFromReader(dataReader);
            }
            return orderItemList;
        }

        public static List<OrderItem> GetByKeys(Int32? id, Int32? orderId)
        {
            List<OrderItem> orderItemList = new List<OrderItem>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("OrderItem_GetByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (orderId.HasValue)
                db.AddInParameter(dbCommand, "OrderId", DbType.Int32, orderId);

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                orderItemList = GetEntitiesFromReader(dataReader);
            }
            return orderItemList;
        }

        public static bool DeleteByKeys(Int32? id, Int32? orderId)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("OrderItem_DeleteByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (orderId.HasValue)
                db.AddInParameter(dbCommand, "OrderId", DbType.Int32, orderId);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        private static List<OrderItem> GetEntitiesFromReader(IDataReader dataReader)
        {
            List<OrderItem> orderItemList = new List<OrderItem>();
            while (dataReader.Read())
            {
                orderItemList.Add(GetEntityFromReader(dataReader));
            }
            return orderItemList;
        }

        private static OrderItem GetEntityFromReader(IDataReader dataReader)
        {
            OrderItem orderItem = new OrderItem();
            orderItem.Id = (Int32)dataReader["Id"];
            orderItem.OrderId = (Int32)dataReader["OrderId"];
            orderItem.ProductId = (Int32)dataReader["ProductId"];
            orderItem.ProductName = (String)dataReader["ProductName"];
            orderItem.ProductCode = (String)dataReader["ProductCode"];
            orderItem.ProductDescription = (String)dataReader["ProductDescription"];
            orderItem.ProductDefaultImage = (String)dataReader["ProductDefaultImage"];
            orderItem.StockId = (Int32)dataReader["StockId"];
            orderItem.StockDescription = (String)dataReader["StockDescription"];
            orderItem.UnitPrice = (Double)dataReader["UnitPrice"];
            orderItem.UnitWeight = (Double)dataReader["UnitWeight"];
            orderItem.Quantity = (Int32)dataReader["Quantity"];
            return orderItem;
        }
    }
}

